/**
   Copyright [Shan Yin]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.monkeydb.inter.http;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.monkeydb.core.MonkeyDB;

public class MonkeySerlvet extends HttpServlet {
	private static final long serialVersionUID = 2855646449821840875L;
	private static Log logger = LogFactory.getLog(MonkeySerlvet.class);
	private MonkeyDB monkeyDB;

	/**
	 * @return the monkeyDB
	 */
	public MonkeyDB getMonkeyDB() {
		return monkeyDB;
	}

	/**
	 * @param monkeyDB the monkeyDB to set
	 */
	public void setMonkeyDB(MonkeyDB monkeyDB) {
		this.monkeyDB = monkeyDB;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {
		String action = req.getParameter("action");
		
		if("get".equalsIgnoreCase(action)){
			onGet(req,res);
		}
		else if("set".equalsIgnoreCase(action)){
			onSet(req,res);
		}
		else if("delete".equalsIgnoreCase(action)){
			onDelete(req,res);
		}
	}
	
	void responseOK(HttpServletResponse res) throws IOException{
		res.getWriter().print("OK");
	}

	/**
	 * @param req
	 * @param res
	 * @throws IOException 
	 */
	private void onDelete(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String key = req.getParameter("key");
		monkeyDB.delete(key);
		responseOK(res);
	}

	/**
	 * @param req
	 * @param res
	 * @throws IOException 
	 */
	private void onSet(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String key = req.getParameter("key");
		String value = req.getParameter("value");
		long ttl = 0;
		try{
			String tmp = req.getParameter("ttl");
			if(tmp!=null){
				ttl = Long.parseLong(tmp);
			}
		}
		catch(Exception e){
			logger.error("Bad ttl", e);
			ttl = 0;
		}
		monkeyDB.set(key, value, ttl);
		responseOK(res);
	}

	/**
	 * @param req
	 * @param res
	 * @throws IOException 
	 */
	private void onGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
		String key = req.getParameter("key");
		String value = monkeyDB.get(key);
		if(value!=null){
			res.getWriter().print(value);
		}
		else{
			res.sendError(404);
		}
	}
}
